package com.ruoyi.web.controller.jl.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.base.Result;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.jl.domain.*;
import com.ruoyi.jl.service.*;
import lombok.extern.slf4j.Slf4j;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/ql")
public class JlGameController extends BaseController {
    private final IPlayerService playerService;

    @Autowired
    IGameService gameService;

    @Autowired
    IGameTableService gameTableService;

    @Autowired
    IGameLogService gameLogService;

    @Autowired
    INoticeService noticeService;

    @Autowired
    IGiveGoldService giveGoldService;

    @Autowired
    IGameProfitService gameProfitService;

    @Autowired
    public JlGameController(IPlayerService playerService) {
        this.playerService = playerService;
    }

    /**
     * 玩家登陆
     *
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestBody Map<String, String> params) {
        Result result = checkParam(params, "nickname", "avatar", "token", "loginIp", "wxOpenid");
        if (result != null) {
            return result;
        }
        Player player = new Player();
        player.setNickname(params.get("nickname"));
        player.setAvatar(params.get("avatar"));
        player.setToken(params.get("token"));
        player.setLoginIp(params.get("loginIp"));
        player.setWxOpenid(params.get("wxOpenid"));
        Player playerInfo = playerService.queryPlayerInfo(player);
        return Result.success(playerInfo);
    }

    @RequestMapping("/queryPlayerInfo")
    @ResponseBody
    public Result queryPlayerInfo(@RequestBody Map<String, String> params) {
        String playerId = params.get("playerId");
        Player player = playerService.queryPlayer(playerId);
        return Result.success(player);
    }

    /**
     * 修改token,修改金币
     *
     * @return
     */
    @RequestMapping("/updatePlayerInfo")
    @ResponseBody
    public Result updatePlayerInfo(@RequestBody Map<String, String> params) {
        Result result = checkParam(params, "playerId", "token");// "carryGold", "newToken"
        if (result != null) {
            return result;
        }
        Player player = new Player();
        player.setPlayerId(Integer.valueOf(params.get("playerId")));
        player.setToken(params.get("token"));
        player.setCarryGold(getIntParam(params, "carryGold"));
        String newToken = params.get("newToken");
        player.setIsOnline(getIntParam(params, "isOnline"));
        playerService.updatePlayerInfo(player, newToken);
        return Result.success();
    }

    /**
     * 查询游戏信息
     *
     * @param params
     * @return
     */
    @RequestMapping("/queryGameInfo")
    @ResponseBody
    public Result queryGameInfo(@RequestBody Map<String, String> params) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("status", 0);
        if (params.containsKey("gameId")) {// 是否有游戏id
            String gameId = params.get("gameId");
            wrapper.eq("id", gameId);
            Game game = gameService.getOne(wrapper);
            return Result.success(game);
        } else {// 查询游戏列表
            List<Game> list = gameService.list(wrapper);
            return Result.success(list);
        }
    }

    /**
     * 修改游戏信息
     *
     * @param params
     * @return
     */
    @RequestMapping("/updateGameInfo")
    @ResponseBody
    public Result updateGameInfo(@RequestBody Map<String, String> params) {
        Result result = checkParam(params, "gameId");
        if (result != null) {
            return result;
        }
        Integer gameId = Integer.valueOf(params.get("gameId"));
        Integer onlineNum = getIntParam(params, "onlineNum");
        Integer stock = getIntParam(params, "stock");
        Integer profitGold = getIntParam(params, "profitGold");
        Game game = new Game();
        game.setId(gameId);
        game.setOnlineNum(onlineNum);
        game.setStock(stock);
        game.setProfitGold(profitGold);
        gameService.updateById(game);
        return Result.success();
    }

    /**
     * 查询桌子信息
     *
     * @return
     */
    @RequestMapping("/queryTalbeInfo")
    @ResponseBody
    public Result queryTalbeInfo(@RequestBody Map<String, String> params) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("status", 0, 1);
        if (params.containsKey("tableId")) {// 是否有桌子id
            String tableId = params.get("tableId");
            wrapper.eq("id", tableId);
            GameTable gameTable = gameTableService.getOne(wrapper);
            return Result.success(gameTable);
        } else {
            // 游戏id和倍数查找
            if (!Strings.isNullOrEmpty(params.get("gameId"))) {
                wrapper.eq("belong_game", params.get("gameId"));
            }
            if (!Strings.isNullOrEmpty(params.get("multiple"))) {
                wrapper.eq("belong_multiple", params.get("multiple"));
            }
            List<GameTable> list = gameTableService.list(wrapper);
            return Result.success(list);
        }

    }

    /**
     * 修改奖池, 修改用户ID, 修改昵称, 修改状态
     *
     * @return
     */
    @RequestMapping("/updateTableInfo")
    @ResponseBody
    public Result updateTableInfo(@RequestBody Map<String, String> params) {
        Result result = checkParam(params, "id", "playerId", "playerNickname", "prizePool", "status");
        if (result != null) {
            return result;
        }
        GameTable gameTable = new GameTable();
        gameTable.setId(Integer.valueOf(params.get("id")));
        gameTable.setPlayerId(Integer.valueOf(params.get("playerId")));
        gameTable.setPlayerNickname(params.get("playerNickname"));
        gameTable.setStatus(Integer.valueOf(params.get("status")));
        Integer prizePool = Integer.valueOf(params.get("prizePool"));
        gameTable.setPrizePool(prizePool == 0 ? null : prizePool);
        gameTableService.updateTableInfo(gameTable);
        return Result.success();
    }

    /**
     * 保存游戏日志
     *
     * @param params
     * @return
     */
    @PostMapping("/saveGameLog")
    @ResponseBody
    public Result saveGameLog(@RequestBody Map<String, String> params) {
        Result result = checkParam(params, "playerId", "gameId", "profitGold", "useChannel");
        if (result != null) {
            return result;
        }
        GameLog gameLog = new GameLog();
        gameLog.setPlayerId(Integer.valueOf(params.get("playerId")));
        gameLog.setGameId(Integer.valueOf(params.get("gameId")));
        gameLog.setProfitGold(Integer.valueOf(params.get("profitGold")));
        gameLog.setUseChannel(params.get("useChannel"));
        gameLog.setCreateTime(new Date());
        gameLogService.save(gameLog);
        return Result.success();
    }

    /**
     * 0公告信息 1客服信息
     *
     * @return
     */
    @PostMapping("/queryNotice")
    @ResponseBody
    public Result queryNotice(@RequestBody Map<String, String> params) {
        Result result = checkParam(params, "type");
        if (result != null) {
            return result;
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type", params.get("type"));
        wrapper.last("limit 1");
        Notice notice = noticeService.getBaseMapper().selectOne(wrapper);
        return Result.success(notice);
    }

    /**
     * 玩家赠送金币
     *
     * @return
     */
    @PostMapping("/playGiveGold")
    @ResponseBody
    public Result playGiveGold(HttpServletRequest request,
                               @RequestBody Map<String, String> params) {
        Result result = checkParam(params, "playerId", "receiveId", "goldNum", "safePwd");
        if (result != null) {
            return result;
        }
        String ipAddr = IpUtils.getIpAddr(request);
        try {
            Integer giveId = Integer.valueOf(params.get("playerId"));
            Integer receiveId = Integer.valueOf(params.get("receiveId"));
            Integer goldNum = Integer.valueOf(params.get("goldNum"));
            String safePwd = params.get("safePwd");
            playerService.PlayGiveGold(giveId, receiveId, goldNum, ipAddr, safePwd);
        } catch (CustomException e) {
            return e.getResult();
        }
        return Result.success();
    }

    /**
     * 修改银行密码
     *
     * @param params
     * @return
     */
    @PostMapping("/updateSafeGold")
    @ResponseBody
    public Result updateSafeGold(@RequestBody Map<String, String> params) {
        try {
            Result result = checkParam(params, "playerId", "oldSafePwd", "newSafePwd");
            if (result != null) {
                return result;
            }
            Integer playerId = Integer.valueOf(params.get("playerId"));
            String oldSafePwd = params.get("oldSafePwd");
            String newSafePwd = params.get("newSafePwd");
            playerService.updateSafeGold(playerId, oldSafePwd, newSafePwd);
        } catch (CustomException e) {
            return e.getResult();
        }
        return Result.success();
    }

    /**
     * 查询玩家日志记录
     *
     * @return
     */
    @PostMapping("/queryGiveLog")
    @ResponseBody
    public Result queryGiveLog(@RequestBody Map<String, String> params) {
        Integer playerId = Integer.valueOf(params.get("playerId"));
        List<GiveGold> list = giveGoldService.queryGiveLog(playerId);
        return Result.success(list);
    }

    /**
     * 保存游戏税收记录
     *
     * @param params
     * @return
     */
    @PostMapping("/saveGameProfit")
    @ResponseBody
    public Result saveGameProfit(@RequestBody Map<String, String> params) {
        Result result = checkParam(params, "gameId", "gold");
        if (result != null) {
            return result;
        }
        String gameId = params.get("gameId");
        String gold = params.get("gold");
        GameProfit gameProfit = new GameProfit();
        gameProfit.setGameId(Integer.valueOf(gameId));
        gameProfit.setGold(Integer.valueOf(gold));
        gameProfit.setCreateTime(new Date());
        gameProfitService.save(gameProfit);
        return Result.success();
    }

    /**
     * 存入,取出银行金币
     *
     * @param params
     * @return
     */
    @PostMapping("/accessBankGold")
    @ResponseBody
    public Result accessBankGold(@RequestBody Map<String, String> params) {
        Result result = checkParam(params, "accessGold", "type", "safePwd");
        if (result != null) {
            return result;
        }
        Integer playerId = Integer.valueOf(params.get("playerId"));
        Integer accessGold = Integer.valueOf(params.get("accessGold"));
        Integer type = Integer.valueOf(params.get("type"));
        String safePwd = params.get("safePwd");

        try {
            gameProfitService.accessBankGold(playerId, accessGold, type, safePwd);
        } catch (CustomException e) {
            return e.getResult();
        }
        return Result.success();
    }

    @RequestMapping("/goldCount")
    @ResponseBody
    public Integer goldCount(HttpServletRequest request) {
        String playerId = request.getParameter("playerId");
        if (Strings.isNullOrEmpty(playerId)) {
            return 0;
        }
        return gameLogService.countGold(playerId);
    }

    @RequestMapping("/queryWrapper")
    @ResponseBody
    public Result queryWrapper(Player player) {
        try {
            playerService.customQueryWrapper(player);
        } catch (CustomException e) {
            return e.getResult();
        }
        return Result.success();
    }

}
